unit FormInputBox;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, cxLabel, cxControls, cxContainer, cxEdit, cxTextEdit,
  cxMaskEdit, cxSpinEdit, Buttons;

type
  TfrmInputBox = class(TForm)
    seValue: TcxSpinEdit;
    lPromt: TcxLabel;
    bDone: TBitBtn;
    bCancel: TBitBtn;
    procedure bDoneClick(Sender: TObject);
    procedure bCancelClick(Sender: TObject);
  private
    { Private declarations }
  public
    fValue : Double;
    constructor Create(Sender: TComponent; const ACaption, APrompt: WideString; ADefault: Double; Min: Double = 0; Max: Double = High(Integer); Increment: double = 1); reintroduce;
  end;

function InputBox(const ACaption, APrompt: WideString; ADefault: Double = 0; Min: Double = 0; Max: Double = High(Integer); Increment: double = 1): double;
function InputBoxInt(const ACaption, APrompt: WideString; ADefault: Integer = 0; Min: Integer = 0; Max: Integer = High(Integer); Increment: Integer = 1): Integer;

var
  frmInputBox: TfrmInputBox;

implementation
{$R *.dfm}
function InputBoxInt(const ACaption, APrompt: WideString; ADefault: Integer = 0; Min: Integer = 0; Max: Integer = High(Integer); Increment: Integer = 1): Integer;
var
    Form : TfrmInputBox;
begin
    Form := TfrmInputBox.Create(nil,ACaption,APrompt,ADefault,Min,Max,Increment);
    Form.seValue.Properties.ValueType := vtInt;
    Form.ShowModal;
    Result := Trunc(Form.fValue);
    Form.Free;
end;


function InputBox(const ACaption, APrompt: WideString; ADefault: Double = 0; Min: Double = 0; Max: Double = High(Integer); Increment: double = 1): double;
var
    Form : TfrmInputBox;
begin
    Form := TfrmInputBox.Create(nil,ACaption,APrompt,ADefault,Min,Max,Increment);
    Form.ShowModal;
    Result := Form.fValue;
    Form.Free;
end;

{ TfrmInputBox }

constructor TfrmInputBox.Create(Sender: TComponent; const ACaption, APrompt: WideString; ADefault: Double; Min: Double = 0; Max: Double = High(Integer); Increment: double = 1);
begin
    inherited Create(Sender);
    Caption                      := ACaption;
    lPromt.Caption               := APrompt;
    fValue                        := ADefault;
    seValue.Value                := ADefault;
    seValue.Properties.MaxValue  := Max;
    seValue.Properties.MinValue  := Min;
    seValue.Properties.Increment := Increment;
end;

procedure TfrmInputBox.bDoneClick(Sender: TObject);
begin
   fValue := seValue.Value;
   Close;
end;

procedure TfrmInputBox.bCancelClick(Sender: TObject);
begin
    fValue := -1;
    Close;
end;

end.
